<?php

class html5av_track {

    private $ID = false;
    private $order_no;
    private $kind;
    private $external_mode;
    private $external_url;
    private $upload_path;
    private $upload_filename;
    private $srclang;
    private $label;
    private $disabled;
    private $audio_video_id;

    public function getID() {
        return (int) $this->ID;
    }

    public function getOrderNo() {
        return (int) $this->order_no;
    }

    public function setOrderNo($order_no) {
        if (is_numeric($order_no))
            $this->order_no = (int) $order_no;
    }

    public function getKind() {
        return $this->kind;
    }

    public function setKind($kind) {
        $this->kind = $kind;
    }

    public function getExternalMode() {        
        if((int) $this->external_mode) {
            return true;
        } else {
            return false;
        }
    }

    public function setExternalMode($external_mode) {
        if($external_mode) {
            $this->external_mode = '1';
        } else {
            $this->external_mode = '0';
        }
    }

    public function getExternalUrl() {
        return $this->external_url;
    }

    public function setExternalUrl($external_url) {
        $this->external_url = $external_url;
    }

    public function getUploadPath() {
        return $this->upload_path;
    }

    public function setUploadPath($upload_path) {
        $this->upload_path = $upload_path;
    }

    public function getUploadFilename() {
        return $this->upload_filename;
    }

    public function setUploadFilename($upload_filename) {
        $this->upload_filename = $upload_filename;
    }

    public function getSrclang() {
        return $this->srclang;
    }

    public function setSrclang($srclang) {
        $this->srclang = $srclang;
    }

    public function getLabel() {
        return $this->label;
    }

    public function setLabel($label) {
        $this->label = $label;
    }

    public function getDisabled() {
        if((int) $this->disabled) {
            return true;
        } else {
            return false;
        }
    }

    public function setDisabled($disabled = true) {
        if($disabled) {
            $this->disabled = '1';
        } else {
            $this->disabled = '0';
        }
    }

    public function getAudioVideoId() {
        return (int) $this->audio_video_id;
    }

    public function setAudioVideoId($audio_video_id) {
        if (is_numeric($audio_video_id))
            $this->audio_video_id = (int) $audio_video_id;
    }

    public function getSrcUrl() {
        if ($this->getExternalMode()) {
            return $this->getExternalUrl();
        } else {
            return html5av_manager::getUploadUrl() . '/' .
            $this->getUploadPath() . '/' .
            $this->getUploadFilename();
        }
    }

    public function getSrc() {
        return html5av_manager::getUploadDir() . '/' .
        $this->getUploadPath() . '/' .
        $this->getUploadFilename();
    }

    public function save() {
        $attr = html5av_dbal::toArray($this, __CLASS__); 
        $db = new html5av_dbal();
        $this->ID = $db->save($this->ID, $attr, 'track');
    }

    public function delete() {
        if ($this->ID) {
            if (!$this->getExternalMode()) {
                unlink($this->getSrc());
            }
            $db = new html5av_dbal();
            $this->ID = $db->delete($this->ID, 'track');
        }
    }

}

class html5av_track_table {

    static public function getByID($id) {
        $db = new html5av_dbal();
        $sql = "SELECT * FROM `" . $db->getPrefix() . "track` WHERE `ID`=?;";
        $prepare = $db->prepare($sql);
        $prepare->execute(array($id));
        return $prepare->fetchObject('html5av_track');
    }

    static public function getByAudioVideoId($audio_video_id, $incDisabled) {
        $db = new html5av_dbal();
        if ($incDisabled) {
            $sql = "SELECT * FROM `" . $db->getPrefix() . "track` WHERE `audio_video_id`=? ORDER BY `order_no` ASC;";
        } else {
            $sql = "SELECT * FROM `" . $db->getPrefix() . "track` WHERE `disabled`=0 AND `audio_video_id`=? ORDER BY `order_no` ASC;";
        }
        $prepare = $db->prepare($sql);
        $prepare->execute(array($audio_video_id));
        $objects = array();
        while ($object = $prepare->fetchObject('html5av_track')) {
            $objects[] = $object;
        }
        return $objects;
    }

}